package com.edushare.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.edushare.entity.Comment;

public class CommentFunctions {

	private class AddComment extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			// getting JSON Object
			JSONObject json = jsonParser.getJSONFromUrl(addCommentURL,
					params[0]);
			return json;
		}
	}

	private class RetriveComment extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			// getting JSON Object
			JSONObject json = jsonParser.getJSONFromUrl(retrieveCommentURL,
					params[0]);
			return json;
		}
	}

	private JSONParser jsonParser;

	// private static String addCommentURL =
	// "http://192.168.1.7/insert_comment.php";
	// private static String retrieveCommentURL =
	// "http://192.168.1.7/retrieve_comment.php";

	private static String addCommentURL = "http://unifiq.5kportal.com/debbieunifiq/insert_comment.php";

	private static String retrieveCommentURL = "http://unifiq.5kportal.com/debbieunifiq/retrieve_comment.php";

	// constructor
	public CommentFunctions() {
		jsonParser = new JSONParser();
	}

	/**
	 * add comment
	 * 
	 * */
	public JSONObject addComment(String username, String groupMediaID,
			String comment) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", username));
		params.add(new BasicNameValuePair("mediaID", groupMediaID));
		params.add(new BasicNameValuePair("comment", comment));

		JSONObject jso = new JSONObject();
		try {
			jso = new AddComment().execute(params).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jso;
	}

	public ArrayList<Comment> retrieveComments(String mediaIDStr) {
		ArrayList<Comment> commentList = new ArrayList<Comment>();
		JSONArray commentJsonArray = new JSONArray();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("mediaID", mediaIDStr));

		JSONObject json = new JSONObject();
		try {
			json = new RetriveComment().execute(params).get();
			int success = json.getInt("success");

			if (success == 1) {
				commentJsonArray = json.getJSONArray("comments");

				for (int i = 0; i < commentJsonArray.length(); i++) {
					JSONObject jObject = commentJsonArray.getJSONObject(i);

					String username = jObject.getString("userID");
					String comment = jObject.getString("comment");

					Comment c = new Comment(comment, username);

					commentList.add(c);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return commentList;
	}

}
